<template>
	{{queryObj.query}}
	<view class="bg-white">
		<view v-for="(item,i) in goodsList" :key="i" @click="gotoDetail(item._id)">
			<myGoods :item="item"></myGoods>
		</view>
		<uni-load-more :status="status"></uni-load-more>
	</view>
</template>

<script setup>
	import {
		computed,
		ref
	} from 'vue';
	import {
		onLoad,
		onReachBottom,
		onPullDownRefresh
	} from '@dcloudio/uni-app'

	const queryObj = ref({
		query: '',
		pagenum: 0,
		pagesize: 10,
		cid: ''
	})

	const goodsList = ref([])
	const total = ref()
	const isloading = ref(false)

	const status = computed(() => {
		if (isloading.value) {
			return "loading"
		}
		if (queryObj.value.pagenum * queryObj.value.pagesize >= total.value) {
			return 'noMore'
		} else {
			return 'more'
		}
	})

	onLoad((e) => {
		queryObj.value.cid = e.cid || ''
		queryObj.value.query = e.query || ''
		getGoodsList()
	})

	onPullDownRefresh(() => {
		queryObj.pagenum = 1
		isloading.value = false
		total.value = 0
		goodsList.value = []

		getGoodsList()
		uni.stopPullDownRefresh()
	})

	onReachBottom(() => {
		if (queryObj.value.pagenum * queryObj.value.pagesize >= total.value) {
			return uni.showToast({
				title: "数据加载完毕!"
			})
		}

		if (isloading.value) return
		queryObj.value.pagenum += 1
		getGoodsList()
	})

	const getGoodsList = async () => {

		isloading.value = true
		const goods = uniCloud.importObject("goodsList")
		const message = await goods.goodsList(queryObj.value)

		console.log(message);

		isloading.value = false

		goodsList.value = [...goodsList.value, ...message.goods]
		total.value = message.total
	}

	const gotoDetail = (goods_id) => {
		uni.navigateTo({
			url: "/subpkg/goods_detail/goods_detail?goods_id=" + goods_id
		})
	}
</script>

<style lang="scss">

</style>